package rollercoaster.railway.utils;

public class ArrayUtils {
	public static boolean transpose(float[][] matrix){
			
			float swap;
			
			for (int i = 0; i < matrix.length; i++) {
				if(matrix.length != matrix[i].length){
					//TODO should generate an exception
					System.out.println("Given matrix is not square matrix");
					return false;
				}
			}
			
			for (int i = 1; i < matrix.length; i++) {
				for (int j = 0; j < i; j++) {
					swap = matrix[i][j];
					matrix[i][j] = matrix[j][i];
					matrix[j][i] = swap;
				}
			}
			
			return true;
		}
	
	public static float[] matrixByVector(float[][] matrix, float[] vector){
		
		for (int i = 0; i < matrix.length; i++) {
			if(matrix.length != vector.length){
				System.out.println("Given matrix and vector dimensions don't match");
				//TODO should generate an exception
				return null;
			}
		}
		
		float[] result = new float[vector.length];
		
		for (int i = 0; i < matrix.length; i++) {
			for (int j = 0; j < matrix[i].length; j++) {
				result[i] += matrix[i][j] * vector[j];
			}
		}
		
		
		return result;
	}
	public static float[][] matrixProduct(float[][] a, float[][] b){
		if(a[0].length!=b.length){
			return null;
		}
		float[][] c=new float[a.length][b[0].length];
		for (int i = 0; i < a.length; i++) {
			for (int j = 0; j < b[0].length; j++) {
				for (int k = 0; k < a[0].length; k++) {
					c[i][j]+=a[i][k]*b[k][j];
				}
				
			}	
		}
		return c;	
	}
	
	public static float[][] fromVecToMat(float[] vec, int l1, int l2){
		if(l1*l2!=vec.length){
			return null;
		}
		float[][] mat=new float[l1][l2];
		int x=0;
		for (int i = 0; i <l1; i++) {
			for (int j = 0; j < l2; j++) {
				mat[j][i]=vec[x];
				x++;
			}
		}
		return mat;
	}
	
	public static float[] fromMatToVec(float[][] mat){
		float[] vec=new float[mat.length*mat[0].length];
		int i=0;
			for (int j = 0; j < mat.length; j++) {
				for (int j2 = 0; j2 < mat[0].length; j2++) {
					vec[i]=mat[j2][j];
					i++;
				}
			}
		return vec;
	}
}
